package com.yxt.qmgf.snake.view;

import com.yxt.qmgf.snake.constant.Const;
import com.yxt.qmgf.snake.vo.SnakeCell;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

public class SnakePane extends Pane {
    private SnakeCell food;
    private Canvas canvas;
    private GraphicsContext context;
    private LinkedList<SnakeCell> cells = Const.SNAKE_CELL_LIST;
    private double score = 0.0;

    private double scorv = 1;

    private int speed = 200; // 默认速度

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getSpeed() {
        return speed;
    }

    public SnakePane() {
        DecimalFormat df = new DecimalFormat("#.0");
        String Score = df.format(score);
        canvas = new Canvas(800, 800);
        context = canvas.getGraphicsContext2D();
        context.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        context.setStroke(Color.BLACK);

        context.strokeRect(50, 50, canvas.getWidth() - 60, canvas.getHeight() - 60);
        context.strokeText("当前分数为：" + Score + " 分",20, 20, 100);
        context.strokeText("speed：" + speed + "ms/次",100, 20, 50);
        getChildren().add(canvas);

    }

    public void drawCell(){

        cells.forEach(cell->{
            context.setFill(cell.getColor());
            context.fillOval(cell.getX(), cell.getY(), cell.getCellLen(), cell.getCellLen());
        });
    }

    public void repaint(){
        DecimalFormat df = new DecimalFormat("#.0");
        String Score = df.format(score);
        context.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        context.setStroke(Color.BLACK);
        context.strokeRect(50, 50, canvas.getWidth() - 60, canvas.getHeight() - 60);
        context.strokeText("当前分数为：" + Score + " 分",50, 20, 200);
        context.strokeText("当前speed：" + speed + " ms/次",200, 20, 200);
        cells.forEach(cell->{
            context.setFill(cell.getColor());
            context.fillOval(cell.getX(), cell.getY(), cell.getCellLen(), cell.getCellLen());
        });
        context.setFill(food.getColor());
        context.fillRect(food.getX(), food.getY(), food.getCellLen(), food.getCellLen());

    }

    public void setScore(double score){
        this.score = score;
    }
    public double getScore(){return score;}
    public void setFood(SnakeCell food){
        this.food = food;
    }

    public SnakeCell getFood(){return food;}

    public double getScorv() {
        return scorv;
    }

    public void setScorv(double scorv) {
        this.scorv = scorv;
    }
}
